﻿namespace UnityEngine
{
    public static class TransformEx
    {
        /// <summary>
        /// 查找子物体,根据子物体名称和组件类型
        /// </summary>
        /// <typeparam name="T">子物体上的某个组件类型</typeparam>
        /// <param name="transform"></param>
        /// <param name="name">子物体名称</param>
        /// <returns></returns>
        public static T FindChild<T>(this Transform transform, string name) where T : Component
        {
            T[] components = transform.GetComponentsInChildren<T>(true);
            for (int i = 0; i < components.Length; i++)
            {
                if (components[i].name == name)
                {
                    return components[i];
                }
            }
            return null;
        }
    }
}
